package org.daochong.lang;

import java.lang.reflect.Method;
import java.math.BigInteger;
import java.util.Date;

public class Validata {

	public boolean isNull(Object o) {
		return o == null;
	}

	public boolean isNotNull(Object o) {
		return o != null;
	}

	public boolean isInt(Object o) {
		if (o == null)
			return false;
		if (o instanceof Integer) {
			return true;
		} else if (o instanceof BigInteger) {
			return true;
		} else if (o instanceof Number) {
			return false;
		}
		String str = o.toString().trim();
		if (str.equals(""))
			return false;
		return str.replaceAll("[\\d]", "").equals("");
	}

	public boolean isDouble(Object o) {
		if (o == null)
			return false;
		if (o instanceof Number) {
			return true;
		}
		String str = o.toString().trim();
		if (str.equals(""))
			return false;
		return str.replaceAll("[\\d]", "").equals(".");
	}

	public boolean isDate(Object o) {
		if (o == null)
			return false;
		if (o instanceof Date) {
			return true;
		}
		String str = o.toString().trim();
		if (str.equals(""))
			return false;
		return str.replaceAll("[\\d]", "").equals(".");
	}

	public boolean between(Object source, Object start, Object end) {
		if (source == null)
			return false;
		if (source instanceof Number) {
			Number n = (Number) source;
			if (start != null) {
				if (start instanceof Number) {
					Number s = (Number) start;
					if (n.doubleValue() < s.doubleValue()) {
						return false;
					}
				} else {
					try {
						if (n.doubleValue() < Double.parseDouble(start.toString())) {
							return false;
						}
					} catch (Throwable e) {
						return false;
					}
				}
			}
			if (end != null) {
				if (end instanceof Number) {
					Number e = (Number) end;
					if (n.doubleValue() > e.doubleValue()) {
						return false;
					}
				} else {
					try {
						if (n.doubleValue() > Double.parseDouble(end.toString())) {
							return false;
						}
					} catch (Throwable e) {
						return false;
					}
				}
			}
			return true;
		} else if (source instanceof Date) {

		}
		return true;
	}

	public boolean in(Object o, Object... values) {
		if (o == null)
			return false;
		for (Object obj : values) {
			if (o.equals(obj)) {
				return true;
			}
		}
		return false;
	}

	public boolean length(String str, int min, int max) {
		if (str == null)
			return false;
		return str.length() >= min && str.length() <= max;
	}

	public static boolean condition(String prop, Object... objs) {
		Validata v = new Validata();
		if ("in".equals(prop)) {
			if (objs == null || objs.length < 2)
				return false;
			Object[] ar = new Object[objs.length-1];
			for(int i=1;i<objs.length;i++){
				ar[i-1] = objs[i];
			}
			return v.in(objs[0], ar);
		} else {
			for (Method m : Validata.class.getMethods()) {
				if (m.getName().equals(prop)) {
					try {
						boolean re = false;
						int len = m.getParameterTypes().length;
						if (len == 1) {
							for (Object o : objs) {
								re = (Boolean) m.invoke(v, o);
								if (!re)
									return false;
							}
						} else {
							re = (Boolean) m.invoke(v, objs);
							if (!re)
								return false;
						}
						return re;
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
			return false;
		}
	}

}
